package yuejian_07.yuejian_07.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.fragment.ClubFragment;
import yuejian_07.yuejian_07.fragment.HomeFragment;
import yuejian_07.yuejian_07.fragment.ShowFragment;
import yuejian_07.yuejian_07.fragment.TrainFragment;
import yuejian_07.yuejian_07.fragment.UserFragment;


public class MainActivity extends FragmentActivity implements OnClickListener {
    private LinearLayout mTabHome;
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabShow;
    private LinearLayout mTabMe;

    private ImageButton mImgHome;
    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgShow;
    private ImageButton mImgMe;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();
        setSelect(0);
    }

    private void initEvent() {
        mTabHome.setOnClickListener(this);
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabShow.setOnClickListener(this);
        mTabMe.setOnClickListener(this);
    }

    private void initView() {
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabShow = (LinearLayout) findViewById(R.id.id_tab_show);
        mTabMe = (LinearLayout) findViewById(R.id.id_tab_me);

        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgShow = (ImageButton) findViewById(R.id.id_tab_show_img);
        mImgMe = (ImageButton) findViewById(R.id.id_tab_me_img);
    }

    private void setSelect(int i)//一个是点击改变图片的背景色
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (mTab01 == null) {
                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                mImgHome.setImageResource(R.drawable.ic_tab_home_pressed);
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                mImgTrain.setImageResource(R.drawable.ic_tab_train_pressed);
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                mImgClub.setImageResource(R.drawable.ic_tab_club_pressed);
                break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new ShowFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                mImgShow.setImageResource(R.drawable.ic_tab_show_pressed);
                break;
            case 4:
                if (mTab05 == null) {
                    mTab05 = new UserFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else {
                    transaction.show(mTab05);
                }
                mImgMe.setImageResource(R.drawable.ic_tab_me_pressed);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);
        }
        if (mTab05 != null) {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_train:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_show:
                setSelect(3);
                break;
            case R.id.id_tab_me:
                setSelect(4);
                break;


            default:
                break;
        }
    }


    private void resetImgs() {
        mImgHome.setImageResource(R.drawable.ic_tab_home_normal);
        mImgTrain.setImageResource(R.drawable.ic_tab_train_normal);
        mImgClub.setImageResource(R.drawable.ic_tab_club_normal);
        mImgShow.setImageResource(R.drawable.ic_tab_show_normal);
        mImgMe.setImageResource(R.drawable.ic_tab_me_normal);
    }
}

